package com.fan.shop.services;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.springframework.transaction.annotation.Transactional;

import com.fan.shop.dao.PurchaseLineDao;
import com.fan.shop.dao.PurchaseOrderDao;
import com.fan.shop.domain.PurchaseLine;
import com.fan.shop.domain.PurchaseOrder;
import com.fan.shop.model.userclass.PurchaseOrderUserClass;

@Named("purchaseOrderService")
@Transactional(readOnly = true)
public class PurchaseOrderServiceImpl implements PurchaseOrderService {

	@Inject
	@Named("purchaseOrderDao")
	private PurchaseOrderDao purchaseOrderDao;

	@Inject
	@Named("purchaseLineDao")
	private PurchaseLineDao purchaseLineDao;

	public PurchaseOrderDao getPurchaseOrderDao() {
		return purchaseOrderDao;
	}

	public void setPurchaseOrderDao(PurchaseOrderDao purchaseOrderDao) {
		this.purchaseOrderDao = purchaseOrderDao;
	}

	public PurchaseLineDao getPurchaseLineDao() {
		return purchaseLineDao;
	}

	public void setPurchaseLineDao(PurchaseLineDao purchaseLineDao) {
		this.purchaseLineDao = purchaseLineDao;
	}

	@SuppressWarnings("rawtypes")
	public List getList() {
		List<PurchaseOrderUserClass> list = new ArrayList<PurchaseOrderUserClass>();
		for(PurchaseOrder purchaseOrder : purchaseOrderDao.getAllPurchaseOrders())
		{
			list.add(copy(purchaseOrder));
		}
		return list;
	}

	public int getColumnsSize() {
		return purchaseOrderDao.getAllPurchaseOrders().size();
	}

	public PurchaseOrderUserClass copy(Object obj) {
		PurchaseOrder purchaseOrder = (PurchaseOrder)obj;
		PurchaseOrderUserClass purchaseOrderUserClass = new PurchaseOrderUserClass();
		purchaseOrderUserClass.setPoNumber(purchaseOrder.getPoNumber());
		purchaseOrderUserClass.setStoreCode(purchaseOrder.getStoreCode());
		purchaseOrderUserClass.setOfficerCode(purchaseOrder.getOfficerCode());
		purchaseOrderUserClass.setTransactionDate(purchaseOrder.getTransactionDate().toString());
		purchaseOrderUserClass.setPostage(purchaseOrder.getPostage());
		purchaseOrderUserClass.setBuyer(purchaseOrder.getBuyer());
		purchaseOrderUserClass.setDeliveryAddress(purchaseOrder.getDeliveryAddress());
		purchaseOrderUserClass.setStatus(purchaseOrder.getStatus());
		purchaseOrderUserClass.setComments(purchaseOrder.getComments());
		purchaseOrderUserClass.setTransactionCode(purchaseOrder.getTransactionCode());
		
		return purchaseOrderUserClass;
	}

	public PurchaseOrder getPurchaseOrder(String poNumber) {
		return purchaseOrderDao.getPurchaseOrder(poNumber);
	}

	public List<PurchaseOrder> getAllPurchaseOrders() {
		return purchaseOrderDao.getAllPurchaseOrders();
	}

	@Transactional(readOnly = false)
	public void addPurchaseOrder(PurchaseOrder purchaseOrder) {
		purchaseOrderDao.addPurchaseOrder(purchaseOrder);
	}

	@Transactional(readOnly = false)
	public void updatePurchaseOrder(PurchaseOrder purchaseOrder) {
		purchaseOrderDao.updatePurchaseOrder(purchaseOrder);
	}
	@Transactional(readOnly = false)
	public void deletePurchaseOrder(PurchaseOrder purchaseOrder) {
		purchaseOrderDao.deletePurchaseOrder(purchaseOrder);
	}

	public boolean hasPurchaseOrder(String poNumber) {
		return purchaseOrderDao.hasPurchaseOrder(poNumber);
	}

	public List<String> getColumnNames() {
		return purchaseOrderDao.getColumnNames();
	}

	public BigDecimal getTotalPrice(String poNumber) {
		return purchaseOrderDao.getTotalPrice(poNumber);
	}

	public int getTotalQty(String poNumber) {
		return purchaseOrderDao.getTotalQty(poNumber);
	}

	public List<PurchaseLine> getPurchaseLines(String poNumber) {
		return purchaseOrderDao.getPurchaseLines(poNumber);
	}

}
